<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 
 */
class account_pay_Controller extends controller_user1{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','账户中心');
		
		$this->assign('account_menu1','3');  
	}
	##################################
	/* 我的账户：begin */
	################################## 
	function index__(){
		factory::module('member');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';
  	  #查询条件
			//$sql_where = ' where 0=0 '.func::speedsearch($_POST['keywords'],array( 'a.remark','a.fee','b.name','c.username','c.name' ));
			$sql_where = ' where a.user_id="'.factory::module('member')->getuserid().'" ';
			
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_trans` a
			left join t_transtype b on a.transtype_id=b.id
			left join t_user c on a.user_id=c.id 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					abs(fee) as fee,
					b.name as transtype_name,
					b.actions,
					c.username,
					c.name as user_name
				from `t_trans` a
				left join t_transtype b on a.transtype_id=b.id
				left join t_user c on a.user_id=c.id 
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
		$this->assign('account_menu2','2');  
		$m_member = factory::module('member');
		$m_trans = factory::module('trans');
		
		$this->assign('rs',$m_member->getuserinfo()); 
		$this->assign('balance',$m_trans->getbalance( $m_member->getuserid() ));
		
		
		
		
		 
		
		
		
	} 
	##################################
	/* 我的账户：end */
	################################## 
	
	
	##################################
	# 在线充值 begin
	##################################
	public function pay__(){ 
	 	$this->assign('nav','在线充值');
		$this->assign('account_menu2','3');  
			
		$m_member = factory::module('member');
		$m_trans = factory::module('trans');
		
		$this->assign('rs',$m_member->getuserinfo()); 
		$this->assign('balance',$m_trans->getbalance( $m_member->getuserid() ));
		
	}
	public function payconfirm__(){ 
	 	$this->assign('nav','在线充值');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$_POST=format::addslash_deep($_POST);
			
			$paytype = isset($_POST['paytype'])?$_POST['paytype']:'';
			//判断充值金额
			$amount = isset($_POST['amount'])?floatval($_POST['amount']):0;
			$amount = number_format($amount, 2, '.', '');
			if($amount <= 0){
				url::msgBox('充值金额必须大于0',url::build('account_pay','pay')); 
			}
			/* 验证 */
			$m_olpay = factory::module('olpay');
			$this->assign('paytype',$paytype);
			$this->assign('paytypedesc',$m_olpay->paytype_desc($paytype));
			$this->assign('amount',$amount);
			/* 支付表单信息获取 */
			$form_info = $m_olpay->webpayform( factory::module('member')->getuserid(),$paytype,$amount);
			
			if($form_info === false){
				url::msgalert($m_olpay->getError());
			}
			$this->assign('form_info',$form_info);
			$this->assign('amount',$amount);
			
			$m_member = factory::module('member');
			$m_trans = factory::module('trans');
			$this->assign('rs',$m_member->getuserinfo()); 
			$this->assign('balance',$m_trans->getbalance( $m_member->getuserid() ));
		}else{
			url::msgBox('参数错误',url::build('account_pay','pay')); 
		}
	}
	//功能函数。将变量值不为空参数组成字符串
	function appendParam($returnStr,$paramId,$paramValue){
		if($returnStr!=""){
			if($paramValue!=""){
				$returnStr.="&".$paramId."=".$paramValue;
			}
		}else{
			if($paramValue!=""){
				$returnStr=$paramId."=".$paramValue;
			}
		}
		return $returnStr;
	}	
	##################################
	# 在线充值 end
	##################################
	 
	 
}
?>